<?php
// Manages a single account's information, as well as aggregate balances.
class Account extends Generic_Record {
    public static function initialize($db) {
        parent::initialize($db,'account');
    }
    
    // returns the account balance just before the transaction was made
    public function get_balance_before_transaction($transaction) {
        $this->get_balance_before_date($transaction->get('date'));
    }
    
    // returns the account balance from before the first transaction on the given date
    public function get_balance_before_date($date) {
        return Account_Aggregate::account_balance($this,$date);
    }
    
    // returns sorted array of transactions for this account (that include account balance after each transaction)
    public function get_transactions($start_date,$end_date = null) {
        $id = $this->get('account_id');
        $start_date = static::escape($start_date);
        $end_clause = ($end_date === null) ? '' : 'AND date < '.static::escape($end_date);
        $where = "(from_account_id = $id OR to_account_id = $id) AND date >= $start_date $end_clause"
        return Transaction::get_transactions($where);
    }
    
}
    
?>